﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace YanZhiwei.DotNet2.Utilities.WinForm
{
    /// <summary>
    /// 委托
    /// </summary>
    /// <param name="state"></param>
    public delegate void CheckBoxClickedHandler(bool state);
    /// <summary>
    /// DataGridViewCheckBoxHeaderCellEventArgs
    /// </summary>
    public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
    {
        #region 构造函数以及变量
        bool ckStatus;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="check">是否选中</param>
        public DataGridViewCheckBoxHeaderCellEventArgs(bool check)
        {
            ckStatus = check;
        }
        /// <summary>
        /// 选中状态
        /// </summary>
        public bool Checked
        {
            get { return ckStatus; }
        }
        #endregion
    }
    /// <summary>
    /// DatagridView 列头绘制Checkbox 帮助类
    /// 参考：http://www.codeproject.com/Articles/20165/CheckBox-Header-Column-For-DataGridView
    ///<para>DatagridViewCheckBoxHeaderCellHelper cbHeader = new DatagridViewCheckBoxHeaderCellHelper();</para> 
    ///<para>gvCheckbox.HeaderCell = cbHeader;</para> 
    ///<para>gvCheckbox.HeaderText = "全选";</para> 
    ///<para>cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);</para> 
    ///<para>void cbHeader_OnCheckBoxClicked(bool state)</para> 
    ///<para>{</para> 
    ///<para> int count = gvPTUID.Rows.Count;</para> 
    ///<para> for (int i = 0; i < count; i++)</para> 
    ///<para> {</para> 
    ///<para>     DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)gvPTUID.Rows[i].Cells[0];</para> 
    ///<para>     checkCell.Value = state;</para> 
    ///<para> }</para> 
    ///<para>}</para> 
    /// </summary>
    public class DatagridViewCheckBoxHeaderCellHelper : DataGridViewColumnHeaderCell
    {
        #region 构造函数以及变量
        Point checkBoxLocation;
        Size checkBoxSize;
        bool ckstatus = false;
        Point cellLocation = new Point();
        System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
        /// <summary>
        /// 勾选事件
        /// </summary>
        public event CheckBoxClickedHandler OnCheckBoxClicked;
        /// <summary>
        /// 构造函数
        /// </summary>
        public DatagridViewCheckBoxHeaderCellHelper()
        {
        }
        #endregion
        #region Paint 重写
        /// <summary>
        /// Paint 重写
        /// </summary>
        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            Point _point = new Point();
            Size _size = CheckBoxRenderer.GetGlyphSize(graphics,
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
            _point.X = cellBounds.Location.X +
                (cellBounds.Width / 2) - (_size.Width / 2);
            _point.Y = cellBounds.Location.Y +
                (cellBounds.Height / 2) - (_size.Height / 2);
            cellLocation = cellBounds.Location;
            checkBoxLocation = _point;
            checkBoxSize = _size;
            if (ckstatus)
                _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.CheckedNormal;
            else
                _cbState = System.Windows.Forms.VisualStyles.
                    CheckBoxState.UncheckedNormal;
            CheckBoxRenderer.DrawCheckBox
            (graphics, checkBoxLocation, _cbState);
        }
        #endregion
        #region  OnMouseClick 重写
        /// <summary>
        /// OnMouseClick 重写
        /// </summary>
        /// <param name="e">DataGridViewCellMouseEventArgs</param>
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            Point _point = new Point(e.X + cellLocation.X, e.Y + cellLocation.Y);
            if (_point.X >= checkBoxLocation.X && _point.X <= checkBoxLocation.X + checkBoxSize.Width && _point.Y >= checkBoxLocation.Y && _point.Y <= checkBoxLocation.Y + checkBoxSize.Height)
            {
                ckstatus = !ckstatus;
                if (OnCheckBoxClicked != null)
                {
                    OnCheckBoxClicked(ckstatus);
                    this.DataGridView.InvalidateCell(this);
                }
            }
            base.OnMouseClick(e);
        }
        #endregion
    }
}

